package com.yaozheng.vocationaltraining.fragment;

import java.util.Random;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import android.text.Html;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.iview.IIntegralLotteryView;
import com.yaozheng.vocationaltraining.iview.integral.IMyIntegralView;
import com.yaozheng.vocationaltraining.service.IntegralLotteryService;
import com.yaozheng.vocationaltraining.service.MyIntegralService;
import com.yaozheng.vocationaltraining.service.impl.IntegralLotteryServiceImpl;
import com.yaozheng.vocationaltraining.service.impl.MyIntegralServiceImpl;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.view.MyLotteryView2;
import com.yaozheng.vocationaltraining.widget.LuckyPanView;

/**
 * 积分抽奖
 *
 * @author Administrator
 */
@EFragment(R.layout.fragment_integral_lottery)
public class IntegralLotteryFragment extends BaseFragment implements
        IIntegralLotteryView, MyLotteryView2.LotteryMonitor, IMyIntegralView {
    @ViewById
    MyLotteryView2 idLuckypan;
    @ViewById
    TextView myIntegralText;
    int winningIndex;
    @ViewById
    TextView headTitleText;
    @Bean(IntegralLotteryServiceImpl.class)
    IntegralLotteryService integralLotteryService;

    @Bean(MyIntegralServiceImpl.class)
    MyIntegralService myIntegralService;

    Random random = new Random();

    int startDegree;

    String message;

    @AfterViews
    public void initView() {
        myIntegralService.init(this);
        integralLotteryService.init(this);
        headTitleText.setText("抽奖");
        setMyIntegralValue("");
        idLuckypan.setLotteryMonitor(this);
        loadData();
    }

    /**
     * 加载数据
     */
    public void loadData() {
        myIntegralService.getUserScore();
    }

    public void setMyIntegralValue(String integral) {
        myIntegralText.setText(integral);
    }


    @UiThread
    @Override
    public void userScoreSuccess(JSONObject jsonObject) {
        JSONObject integralJsonObject = TypeUtils.getJsonObject(jsonObject,
                "data", "score");
        setMyIntegralValue(TypeUtils.getJsonString(integralJsonObject, "quarter"));

    }

    @UiThread
    @Override
    public void userScoreError(String error) {
        cancelLoadDataProgressDialog();
        alertMessage(error);
    }

    @UiThread
    @Override
    public void lotterySuccess(JSONObject jsonObject) {
        cancelOperatingProgressDialog();
//        {
//            "error_code": 0,
//                "data": {
//            "award": {
//                "index": 6,
//                        "score": 0,
//                        "name": "谢谢参与"
//            }
//        }
//        }

        JSONObject awardJsonObject = TypeUtils.getJsonObject(jsonObject, "data", "award");
        int index = TypeUtils.getJsonInteger(awardJsonObject, "index", -1);
        if (index == -1) {
            alertMessage("服务器返回数据有问题");
        } else {
            //服务器数据 跟我数据是反的 (LOTTERY_STR.length-1)-index
            startDegree = (MyLotteryView2.LOTTERY_STR.length - 1) - index;
            message = TypeUtils.getJsonString(awardJsonObject, "name");
            // (random.nextInt(MyLotteryView2.LOTTERY_STR.length));
            idLuckypan.beginLottery(startDegree);
        }
    }

    @UiThread
    @Override
    public void lotteryError(String error) {
        cancelOperatingProgressDialog();
        alertMessage(error);
    }

    @Override
    public void lotteryEnd() {
//        alertMessage(String.valueOf(MyLotteryView2.LOTTERY_STR[startDegree]));
        showCancelAskDialog(message);
        loadData();
    }

    @Override
    public void lotteryBegin() {
//        showOperatingProgressDialog("取数据中...");
        integralLotteryService.lottery();
    }


}
